package com.zl.smartmall.library.listener;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zl.smartmall.library.po.AssortFirstLevelInfo;
import com.zl.smartmall.library.util.CrashHandler;

/**
 * 第一级请求监听类
 * 
 * @author 陈希然
 * 
 */
public abstract class GetAssortFirstlevelListener extends BaseListener {

	private static final String TAG = GetAssortFirstlevelListener.class
			.getSimpleName();

	@Override
	public void onOffline() {
	}
	@Override
	public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
		try {
			int ret = response.getInt("ret");
			switch (ret) {
			case 0:
				JSONObject result = response.getJSONObject("result");
				JSONArray listArray = result.getJSONArray("one_kinds");
				JSONArray building_list = result.getJSONArray("two_kinds");
				List<AssortFirstLevelInfo> lists = new ArrayList<AssortFirstLevelInfo>();
				for (int i = 0; i < listArray.length(); i++) {
					JSONObject provinceJson = listArray.getJSONObject(i);
					AssortFirstLevelInfo info = new AssortFirstLevelInfo();
					info.setId(provinceJson.getInt("id"));
					info.setName(provinceJson.getString("name"));
					info.setPic(provinceJson.getString("pic"));
					info.setCmid(provinceJson.getInt("cmid"));
					info.setParentid(provinceJson.getInt("parent_id"));
					info.setLevel(provinceJson.getInt("level"));
					info.setStatus(provinceJson.getInt("status"));
					info.setCreatedat(provinceJson.getString("created_at"));
					info.setUpdatedat(provinceJson.getString("updated_at"));
					info.setKindorder(provinceJson.getInt("kind_order"));
					lists.add(info);
					for (int j = 0; j < building_list.length(); j++) {
						JSONObject cityJson = building_list.getJSONObject(j);
						AssortFirstLevelInfo city = new AssortFirstLevelInfo();
						if (cityJson.getInt("parent_id")==provinceJson.getInt("id")) {
							city.setId(cityJson.getInt("id"));
							city.setName(cityJson.getString("name"));
							city.setPic(cityJson.getString("pic"));
							city.setCmid(cityJson.getInt("cmid"));
							city.setParentid(cityJson.getInt("parent_id"));
							city.setLevel(cityJson.getInt("level"));
							city.setStatus(cityJson.getInt("status"));
							city.setCreatedat(cityJson.getString("created_at"));
							city.setUpdatedat(cityJson.getString("updated_at"));
							city.setKindorder(cityJson.getInt("kind_order"));
							info.getSecondLists().add(city);
						}
					}
				}
				onResponseSuccess(lists);
				break;
			case 2:
				onOffline();
				break;
			case 4:
				onNoMoreData();
				break;

			default:
				onResponseFailed();
				break;
			}

		} catch (JSONException e) {
			onFailure(statusCode, headers, e, new JSONObject());
		}
	}

	@Override
	public void onFailure(int statusCode, Header[] headers,
			Throwable throwable, JSONObject errorResponse) {
		// TODO Auto-generated method stub
		CrashHandler.e(throwable);
		onResponseFailed();
	}

	/**
	 * 商品一级列表数据请求成功
	 */
	public abstract void onResponseSuccess(
			List<AssortFirstLevelInfo> maininfInfos);

}
